<?php

namespace app\api\controller;

use app\api\model\UserAuth as UserAuthModel;

class UserAuth extends Controller
{
    private $user;
    private $data;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();   // 用户信息
        $this->data = UserAuthModel::getAuthDetail($this->user['user_id']);
    }

    public function index()
    {
        if(empty($this->data)){
            return $this->renderJson(10001, '资料为空');
        }
        return $this->renderSuccess($this->data);
    }

    public function add(){
        if($this->data){
            return $this->renderError('认证资料已存在');
        }
        $data = $this->postData('auth');
        $auth = new UserAuthModel();
        $data['user_id'] = $this->user['user_id'];
        if($auth->add($data, $this->user)){
            return $this->renderSuccess();
        }
        $error = $auth->getError()?:'提交失败';
        return $this->renderError($error);
    }
}